<template>
  <div id="course">  
    <div  style="display:flex;align-items: center;">
            <img class="top_img" src="@/assets/images/a8.png" style="width:35px;height:35px;margin-right:10px;"/>
            <div style="line-height: 40px;font-weight:bold:">每日分享</div>
     </div>
    <el-row>
        <el-col :span="24" style="text-align: right;">         
            <el-button type="primary" class="custom_color_button1" style="margin-bottom:10px;" @click="add_but">
              <img class="custom_color_button1_img" src="@/assets/images/but_bg2.png" />添加信息
            </el-button>         
        </el-col>
  </el-row>

   <!-- 渲染数据list -->
   <everyday-list :datalist="datalist" @up_list_fun="list_fun"> </everyday-list>

      
   <div style="margin-top:20px;text-align: center;">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :page-size="10"  
        layout="prev, pager, next, jumper"
        :total="count">
      </el-pagination>
    </div>
   

      <!-- 添加内容 -->
    <el-dialog title="添加内容" :visible.sync="add_mtk"  width="600px">
        <div style="width:100%;"> 


        <el-form ref="form"   label-width="70px" style="width:100%;">
    
             <el-form-item label="选择日期" >
                    <el-date-picker type="date" style="width:100%;"  placeholder="选择日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"  v-model="mtk_form.date" ></el-date-picker>
              </el-form-item> 

              <el-form-item label="每日文案">
                 <el-input type="textarea" v-model="mtk_form.writing" maxlength="70"></el-input>
               </el-form-item>

                 <el-form-item label="上传图片">
                    <el-upload class="upload-demo" action="" drag 
                            :auto-upload="false" :show-file-list="false" :on-change='changeUpload'>
                                    <i class="el-icon-upload"></i>
                                    <div class="el-upload__text" style="margin-top:0px;">点击上传</div>
                                    <div class="el-upload__tip" style="margin-top:-10px;">单张图片最大支持5MB,图片大小588*588</div>
                    </el-upload>
                  </el-form-item>
                  <div style="text-align: center;margin-bottom:10px;">

                       <img :src="pic_form.head_img" style="width:400px;" />
                  </div>
            </el-form>

            <div style="text-align: center;width:50%;margin:auto;">
                <el-button type="primary" class="custom_color_button" style="width:80%;" @click="add_but_qr">确认</el-button>
            </div>

        </div> 
    </el-dialog>


     <!-- vueCropper 剪裁图片实现-->
    <el-dialog title="图片剪裁" :visible.sync="dialogVisible" append-to-body>
      <div class="cropper-content">
        <div class="cropper" style="text-align:center">
        <vueCropper
            ref="cropper"
            :img="option.img"
            :outputSize="option.size"
            :outputType="option.outputType"
            :info="true"
            :full="option.full"
            :canMove="option.canMove"
            :canMoveBox="option.canMoveBox"
            :original="option.original"
            :autoCrop="option.autoCrop"
            :fixed="option.fixed"
            :fixedNumber="option.fixedNumber"
            :centerBox="option.centerBox"
            :infoTrue="option.infoTrue"
            :fixedBox="option.fixedBox"
          ></vueCropper>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="finish" :loading="loading">确认</el-button>
      </div>
    </el-dialog>
    

      
  </div>
</template>

<script>
import EverydayList from '@/view/Infor/EverydayList'
export default {
  name: 'course',
  data () {
    return {
        datalist:[],
        page:1,
        count:0,
        add_mtk:false,
        mtk_form:{
            writing:'',
            date:''
        },
         pic_form:{
           head_img:'',
           upfile:'',
        },
        dialogVisible: false,
      // 裁剪组件的基础配置option
      option: {
        img: '', // 裁剪图片的地址
        info: true, // 裁剪框的大小信息
        outputSize: 0.8, // 裁剪生成图片的质量
        outputType: 'jpeg', // 裁剪生成图片的格式
        canScale: false, // 图片是否允许滚轮缩放
        autoCrop: true, // 是否默认生成截图框
        //autoCropWidth: 588, // 默认生成截图框宽度
        //autoCropHeight: 588, // 默认生成截图框高度
        fixedBox: false, // 固定截图框大小 不允许改变
        fixed: true, // 是否开启截图框宽高固定比例
        fixedNumber: [1, 1], // 截图框的宽高比例
        full: true, // 是否输出原图比例的截图
        canMoveBox: false, // 截图框能否拖动
        original: false, // 上传图片按照原始比例渲染
        centerBox: false, // 截图框是否被限制在图片里面
        infoTrue: true // true 为展示真实输出图片宽高 false 展示看到的截图框宽高
      },
      picsList: [],  //页面显示的数组
      // 防止重复提交
      loading: false
    }
  },
 components:{
    EverydayList
  },
  created(){
     this.list_fun()
  },
  mounted (){
    
  },
  methods: {
      list_fun(){
          this.post("/share/listOfShareDay", {
              page:this.page,
	        }).then(res => {
            this.count = res.result.count
            this.datalist = res.result.list
            for(let item of this.datalist) {
               
                item.iconlist = [this.urlimg + item.icon]

                
                          
             }
            console.log(res)
         })
      },
      add_but(){
           this.add_mtk = true
      },
      add_but_qr(){
          this.post("/share/addShareOfDay", {
                date:this.mtk_form.date,
                writing:this.mtk_form.writing,
                icon:this.pic_form.upfile
            }).then(res => {

                if(res.error_code == 0){
                    this.open1(res.result,'success')  //提示框
                    this.list_fun()
                    this.add_mtk = false
                }else {
                this.open1(res.message,'warning')
            }

        })
      },
       // 上传按钮   限制图片大小
    changeUpload(file, fileList) {
      const isLt5M = file.size / 1024 / 1024 < 5
      if (!isLt5M) {
        this.$message.error('上传文件大小不能超过 5MB!')
        return false
      }
      this.fileinfo = file
      console.log(file,"222")
      // 上传成功后将图片地址赋值给裁剪框显示图片
   
      this.$nextTick(() => {
       
         var windowURL = window.URL || window.webkitURL;               
        this.option.img  = windowURL.createObjectURL(file.raw); 
        this.dialogVisible = true
      })
    },
    finish() {
      this.$refs.cropper.getCropBlob((data) => {
        //var fileName = 'goods' + this.fileinfo.uid
        this.loading = true
        //data.name = "aaaa.png"
        console.log(data)
       //上传阿里云服务器           
       
        let aTime = new Date().getTime() // 取时间戳，给文件命名
        let fileName = aTime + '.' + data.type.substr(6) // 给文件名加后缀
        let file = new window.File([data], fileName, { type: data.type }) // blob转file

            this.upimgpost("/image/uploadMultiple",file).then(res => {
                console.log(res.result.url )
                this.pic_form.head_img= this.urlimg + res.result.url 
                this.pic_form.upfile = res.result.url
                this.dialogVisible = false 
                this.loading = false
                    
            })

      })
    },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.page = val
        this.list_fun()
        console.log(`当前页: ${val}`);
      }
  }
}
</script>


<style scoped>
.avatar-uploader  {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
  .avatar {
    width: 150px;
    height: 150px;
    display: block;
  }
 
    .cropper {
        width: auto;
        height: 600px;
    }

</style>
